/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.CarModelDTO;
import east2west.interfaces.CarModelInterface;
import east2west.util.DatabaseUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author anhtuan
 */
public class CarModelDAO implements CarModelInterface {

    Connection con = null;
    CarModelDTO carmodel = null;
    CallableStatement call = null;
    ResultSet rs = null;
    private final String SQL_READ_BY_ALL = "select * from V_CarModels";
    private final String SQL_READ_BY_ID = "{call sp_Call_View_CarModels_FindId (?)}";
    private final String SQL_READ_BY_NAME = "{call sp_Call_View_CarModels_FindName (?)}";
    private final String SQL_CREATE = "{call sp_Insert_CarModels (?)}";
    private final String SQL_UPDATE = "{call sp_Update_CarModels (?,?)}";
    private final String SQL_DELETE = "{call sp_Delete_CarModels (?)}";

    public ArrayList<CarModelDTO> readByAll() {
       ArrayList<CarModelDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_READ_BY_ALL);
            rs = call.executeQuery();
            list = new ArrayList<CarModelDTO>();
            while (rs.next()) {
                carmodel = new CarModelDTO();
                carmodel.setModelId(rs.getInt(1));
                carmodel.setModelName(rs.getString(2));
                list.add(carmodel);
            }
            rs.close();
            call.close();
            return list;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }

    }

    public CarModelDTO findById(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_READ_BY_ID);
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {
                carmodel = new CarModelDTO();
                carmodel.setModelId(rs.getInt(1));
                carmodel.setModelName(rs.getString(2));
                carmodel.setIsDeleted(rs.getByte(3));
            }
            rs.close();
            call.close();
            return carmodel;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }

    public ArrayList<CarModelDTO> findByName(String name) {
        ArrayList<CarModelDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_READ_BY_NAME);
            call.setString(1, name);
            rs = call.executeQuery();
            list = new ArrayList<CarModelDTO>();
            while (rs.next()) {
                carmodel = new CarModelDTO();
                carmodel.setModelId(rs.getInt(1));
                carmodel.setModelName(rs.getString(2));
                carmodel.setIsDeleted(rs.getByte(3));
                list.add(carmodel);
            }
            rs.close();
            call.close();
            return list;
        } catch (Exception ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }
    }

    public boolean create(CarModelDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_CREATE);
            call.setString(1, dto.getModelName());
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarModelDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean update(CarModelDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_UPDATE);
            call.setInt(1, dto.getModelId());
            call.setString(2, dto.getModelName());
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarModelDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean delete(CarModelDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall(SQL_DELETE);
            call.setInt(1, id);
            call.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            try {
                DatabaseUtil.closeConnection(con);
                call.close();
            } catch (SQLException ex) {
                Logger.getLogger(CarModelDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
